/*
SQL_client_proc.dm
All of the procs related to saving/creation/loading can be found here.

Included:
	create query m 	Internal, runs the initial query on the database to save m
	update_query m	Internal, updates variables whilst saving
	Load			Load a character
	Save			Save a character
*/
client/proc
	create_query(mob/mob)
		var/qr="INSERT INTO `[sql_table]` ("
		for(var/x=1 to mob.vars.len)
			if(isclient(mob.vars[mob.vars[x]]))continue
			if(ignored_vars.Find(mob.vars[x]))
				if(x == mob.vars.len)
					qr = copytext(qr, 1, length(qr))
				continue
			if(x==mob.vars.len)
				qr += "`[mob.vars[x]]`"
			else				//making the initial part of the query
				qr += "`[mob.vars[x]]`"
				qr += ","
		qr += ") VALUES ("
		var/list/VARIABLES=mob.vars
								//giant mess of stuff below

		var/ignored
		for(var/x=1 to VARIABLES.len)
			if(x==(VARIABLES.len))

				if(isclient(VARIABLES[VARIABLES[x]]))continue
				if(ignored_vars.Find(VARIABLES[x]))
					ignored=1
					continue

				if( islist(VARIABLES[VARIABLES[x]]))
					qr += "'[list2params(VARIABLES[VARIABLES[x]])]'"
					continue

				if( istext(VARIABLES[VARIABLES[x]]) || isdatum(VARIABLES[VARIABLES[x]]) || ispath(VARIABLES[VARIABLES[x]]))
					qr += "\"[VARIABLES[VARIABLES[x]]]\""
					continue

				if( isnum(VARIABLES[VARIABLES[x]]) )
					qr += "[VARIABLES[VARIABLES[x]]]"
					continue

				if( isnull(VARIABLES[VARIABLES[x]]))
					qr += "null"
					continue

				else qr += "'BROKEN'"

			else

				if(isclient(VARIABLES[VARIABLES[x]]))continue
				if(ignored_vars.Find(VARIABLES[x]))continue

				if( istext(VARIABLES[VARIABLES[x]]) || isdatum(VARIABLES[VARIABLES[x]]) || ispath(VARIABLES[VARIABLES[x]]))
					qr += "'[VARIABLES[VARIABLES[x]]]',"
					continue

				if( isnum(VARIABLES[VARIABLES[x]]) )
					qr += "[VARIABLES[VARIABLES[x]]],"
					continue

				if( islist(VARIABLES[VARIABLES[x]]))
					qr += "'[list2params(VARIABLES[VARIABLES[x]])]',"
					continue

				if( isnull(VARIABLES[VARIABLES[x]]))
					qr += "null"
					qr += ","
					continue

				else
					qr += "'BROKEN',"
		if(ignored)
			qr = copytext(qr, 1, length(qr))
		qr += ");"
		return qr

	update_query(mob/mob,tvar)
		if(ignored_vars.Find(tvar))return   //this is a query run to do updates
		var/qr="UPDATE `[sql_table]` SET [tvar]= "

		if(isnum(mob.vars[tvar]))
			qr += "[mob.vars[tvar]]"
		else if(islist(mob.vars[tvar]))
			qr += "'[list2params(mob.vars[tvar])]'"
		else
			qr += "'[mob.vars[tvar]]'"

		qr += " WHERE ckey='[mob.ckey]';"
		return qr


	Load()
		char_screen=FALSE

		var/DBQuery/qry = Query("SELECT * FROM `[sql_table]` where `ckey`='[ckey]';")
		if(qry.RowCount() > 0)
			while(qry.NextRow())
				var/list/row_data = qry.GetRowData()
				for(var/D in row_data)
					if(!mob.vars.Find(D)|| ignored_vars.Find(D))continue
					if(findtext(row_data[D],"="))
						mob.vars[D]=params2list(row_data[D])
					else
						if(oddvars.Find(D))mob.vars[D]=text2num(row_data[D])
						else mob.vars[D]=row_data[D]

			var/DBQuery/qry_re = Query("SELECT respawn FROM `[sql_table]` WHERE `ckey`='[mob.ckey]';")
			qry_re.NextRow()
			var/list/rd=qry_re.GetRowData()
			mob.loc=locate(text2path(rd[rd[1]]))
			mob.respawn = text2path(mob.respawn)

			if(mob.iconFile)
				mob.icon = icon(file(mob.iconFile))
			else
				mob.icon='base.dmi'
				mob.icon_state="person"

			var/mob/player/p = mob
			p.update_bars()
			p.create_hud()

			LoadContents()

			p.regen_loop()

		else src << "Sorry, your character never existed."


	Save()
		if(char_screen)return
		mob.tx=mob.x
		mob.ty=mob.y
		mob.tz=mob.z
		var/list/VARIABLES=mob.vars

		for(var/x=1 to VARIABLES.len)
			Query(update_query(mob,mob.vars[x]))

		src << "Done saving."

	LoadContents()
		var/DBQuery/qry = Query("SELECT * FROM `[sql_contentstable]` where owner='[mob.name]';")
		if(qry.RowCount() > 0)
			while(qry.NextRow())
				var/list/row_data = qry.GetRowData()
				var/x = text2path(row_data["type"])
				if(x)
					var/obj/gettable/o = new x
					o.amount = text2num(row_data["amount"])
					o.uid = text2num(row_data["uid"])
					mob.contents += o

					if(row_data["slot"])
						var/obj/gettable/equipment/eq = o
						eq.equip(mob, 1)

			spawn(20)
				var/mob/player/p=mob
				p.update_contents()